package leetcode.weekly.week317;

//Solution3Test
public class Solution3 {

	public long makeIntegerBeautiful(long n, int target) {
		long sum = this.sum(n);
		if (sum <= target) {
			return 0;
		}
		long ans = 0;
		long plus = 1;
		while (sum > target) {
			long last = n % 10;
			long add = 10 - last;
			ans = ans + plus * add;
			plus *= 10;
			n = (n + add) / 10;
			sum = this.sum(n);
		}
		return ans;
	}

	public long sum(long n) {
		long num = n;
		long sum = 0;
		while (num > 0) {
			sum += num % 10;
			num /= 10;
		}
		return sum;
	}
}
